package com.ihr360.cnb.module.insurance.service.impl;

import com.ihr360.authority.embed.base.entity.AuthStaffDataRequest;
import com.ihr360.authority.embed.base.type.AuthorityStaffStatus;
import com.ihr360.cnb.module.common.service.Ihr360StaffApiService;
import com.ihr360.cnb.module.insurance.service.CompanyBenefitAggregateService;
import com.ihr360.cnb.module.insurance.service.feign.InsuranceCompanyBenefitService;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.model.type.BenefitCategory;
import com.ihr360.commons.vo.PageData;
import com.ihr360.insurance.entity.request.SingleStaffBenefitVoRequest;
import com.ihr360.insurance.entity.request.StaffBenefitRequest;
import com.ihr360.insurance.entity.request.StaffBenefitVoRequest;
import com.ihr360.insurance.entity.request.StaffIdRequest;
import com.ihr360.insurance.entity.vo.SingleStaffBenefitVo;
import com.ihr360.insurance.entity.vo.StaffBenefitVo;
import com.ihr360.type.StaffStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author Stone.Shi
 * @description
 * @date 2018-06-06 15:34:06.
 */
@Service
public class CompanyBenefitAggregateServiceImpl implements CompanyBenefitAggregateService {

    @Autowired
    private InsuranceCompanyBenefitService companyBenefitService;

    @Autowired
    private Ihr360StaffApiService staffApiService;

    @Override
    public PageData<StaffBenefitVo> getUnrelatedStaffs(String companyId, String userId,
                                                       StaffBenefitRequest request,
                                                       String authorityCode) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequest(request, authorityCode);
        staffDataRequest.setAuthorityStaffStatus(AuthorityStaffStatus.IN_SERVICE);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return new PageData<>(0, 0, Collections.emptyList());
        }
        request.setStaffIds(authStaffIds);
        return companyBenefitService.getUnrelatedStaffs(companyId, request);
    }

    private AuthStaffDataRequest getStaffDataRequest(StaffBenefitRequest request, String authorityCode) {
        AuthStaffDataRequest staffDataRequest = new AuthStaffDataRequest();
        staffDataRequest.setAuthorityCode(authorityCode);
        if (request != null) {
            staffDataRequest.setStaffName(request.getStaffName());
            staffDataRequest.setIdCardNo(request.getIdCardNo());
            staffDataRequest.setDepartIdList(request.getDepartmentIds());
            StaffStatus staffStatus = request.getStaffStatus();
            if (staffStatus == StaffStatus.IN_SERVICE) {
                staffDataRequest.setAuthorityStaffStatus(AuthorityStaffStatus.IN_SERVICE);
            } else if (staffStatus == StaffStatus.QUIT) {
                staffDataRequest.setAuthorityStaffStatus(AuthorityStaffStatus.QUIT);
            }
        }
        return staffDataRequest;
    }

    @Override
    public int getRelatedStaffCount(String companyId, String userId, BenefitCategory category, String authorityCode) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequest(null, authorityCode);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return 0;
        }
        return companyBenefitService.getRelatedStaffCount(companyId, category, new StaffIdRequest(authStaffIds));
    }

    @Override
    public int getUnRelatedStaffCount(String companyId, String userId, BenefitCategory category, String authorityCode) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequest(null, authorityCode);
        staffDataRequest.setAuthorityStaffStatus(AuthorityStaffStatus.IN_SERVICE);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return 0;
        }
        return companyBenefitService.getUnRelatedStaffCount(companyId, category, new StaffIdRequest(authStaffIds));
    }

    @Override
    public void saveBenefits(String companyId, String userId,
                             List<StaffBenefitVo> benefitVos, BenefitCategory category, String authorityCode) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequest(null, authorityCode);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return;
        }

        companyBenefitService.saveBenefits(companyId, category, new StaffBenefitVoRequest(authStaffIds, benefitVos));
    }

    @Override
    public void saveSingleBenefits(String companyId, String userId,
                                   List<SingleStaffBenefitVo> singleBenefitVos, BenefitCategory category, String authorityCode) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequest(null, authorityCode);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return;
        }
        companyBenefitService.saveSingleBenefits(companyId, category, new SingleStaffBenefitVoRequest(authStaffIds, singleBenefitVos));
    }

    @Override
    public void deleteSingleBenefits(String companyId, String userId,
                                     List<SingleStaffBenefitVo> singleBenefitVos, BenefitCategory category, String authorityCode) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequest(null, authorityCode);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return;
        }
        companyBenefitService.deleteSingleBenefits(companyId, category, new SingleStaffBenefitVoRequest(authStaffIds, singleBenefitVos));
    }
}
